<?php
namespace app\admin\validate;
use think\facade\Db;
use think\Validate;

class Users extends Validate
{

    protected $rule=[
        'id'    =>'require',
        'name'    =>'require',
        'mobile'    =>'require',
        'registermobile'    =>'require|checkuniquemobile|checkmobile',
        'password'    =>'require|min:6',
        'vertify'    =>'require',
        'code'    =>'require',

        'old_mobile'    =>'require|checkmobile',
        'old_code'    =>'require',
        'new_mobile'    =>'require|checkuniquemobile|checkmobile',
        'new_code'    =>'require',

        'old_password'    =>'require',
        'new_password'    =>'require|min:6',
        'forget_mobile'    =>'require',
    ];
    protected $message = [
        'id.require'    => '关键参数不能为空',
        'name.require'    => '姓名不能为空',
        'registermobile.require'    => '手机号码不能为空',
        'registermobile.unique'    => '已存在相同手机号',
        'password.require'    => '请输入密码',
        'password.min'    => '密码最少输入6位',
        'vertify.require'    => '图形验证码不能为空',
        'code.require'    => '短信验证码不能为空',
        'mobile.require'    => '手机号码不能为空',

        'old_mobile.require'    => '旧手机号码不能为空',
        'old_code.require'    => '旧手机号验证码不能为空',
        'new_mobile.require'    => '新手机号码不能为空',
        'new_mobile.unique'    => '新手机号码存在相同手机号',
        'new_code.unique'    => '新手机号验证码不能为空',

        'old_password.require'    => '旧密码不能为空',
        'new_password.require'    => '新密码不能为空',
        'new_password.min'    => '新密码最少输入6位',
        'forget_mobile.require'    => '手机号码不能为空',
        'repassword.require'    => '再次输入密码不能为空',
    ];
    protected $scene = [
        'add' =>['name','mobile'],
        'register' =>['password','registermobile','vertify','code'],
        'login1'=>['mobile','password','vertify'],
        'login2'=>['mobile','vertify','code'],
        'edit'=>['id','name'],
        'user_edit'=>['id','name','mobile'],
        'del' =>['id'],
        'udpate_mobile'=>['old_mobile','old_code','new_mobile','new_code'],
        'udpate_pwd'=>['old_mobile','old_password','new_password'],
        'forgetpwd'=>['forget_mobile','password','repassword'],
    ];

    protected function checkmobile($value,$rule,$data){
        if(!is_numeric($value)){
            return '请输入数字';
        }
        if(strlen($value) != 11){
            return '请输入11位手机号';
        }
        if (!preg_match("/^1[3456789]\d{9}$/", $value)) {
            return '手机号码格式不正确';
        }
        return true;
    }
    protected function checkuniquemobile($value,$rule,$data){
        $one = DB::name('users')->where('mobile','=',$value)->find();
        if($one){
            return '手机号已经存在';
        }
        return true;
    }



}